const Service = require('egg').Service;

class CommentsService extends Service {
    get Comment() {
        return this.app.model.Comments;
    }
    /**
     * 添加评论
     * @param {*} commentinfo 
     * @returns 
     */
    async add(commentinfo) {
        const result = new this.Comment(commentinfo);
        const dbback = await result.save();
        return dbback;
    }
    /**
     * 删除评论
     * @param {*} commentid 
     * @returns 
     */
    async delete(commentid){
        return await this.Comment.findByIdAndRemove(commentid);
    }
    /**
     * h获取评论列表
     * @param {*} page 
     * @param {*} pagesize 
     * @param {*} vodid 
     * @returns 
     */
    async getcommentlist(page,pagesize,videoid){
        const skip = (page - 1) * pagesize
        const dbback = await this.Comment.find({videoid}).skip(skip).limit(pagesize).populate('userid','avater username')
        const count = await this.Comment.countDocuments({videoid})
        return {dbback,count}
    }

    async getcomment(id){
        const dbback = await this.Comment.findById(id)
        return dbback
    }
    
}

module.exports = CommentsService;